package com.example.mysaat.service_menu_manual_test.AllFunctions;

import android.app.Activity;
import android.app.Service;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.annotation.RequiresApi;

public class SIM1 {
    private TelephonyManager phoneManager = null;
    private int simState =0;
    private Activity activity;
    private static Boolean SIM1_RESULT = false;   //1成功   0失败
    private static final String TAG = "SIM1";
    private static int SLOT_INDEX_FIRST = 0;

    public SIM1(Activity activity,int slotindex){
        this.activity = activity;
        this.SLOT_INDEX_FIRST = slotindex;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public boolean testSIM(){
        phoneManager = (TelephonyManager)activity.getSystemService(Service.TELEPHONY_SERVICE);
        if(phoneManager==null){
            Log.d(TAG, "testSIM: TelephoneManager is null");
            return false;
        }

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int numSlots = phoneManager.getPhoneCount();
            // 只有一个卡槽的时候,获取卡槽的状态
            simState = numSlots>1?phoneManager.getSimState(SLOT_INDEX_FIRST):phoneManager.getSimState();
        }
        //当存在SIM卡已知并SIM已填充，则成功
        SIM1_RESULT = simState!=TelephonyManager.SIM_STATE_ABSENT&&simState!=TelephonyManager.SIM_STATE_UNKNOWN?true:false;

        if(SLOT_INDEX_FIRST==0)
        Log.d(TAG, "testSIM1:"+SIM1_RESULT);
        return SIM1_RESULT;
    }
}
